package com.jhgk.jhcee.expert.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jhgk.jhcee.auth.mapper.ScoreWcMapper;
import com.jhgk.jhcee.auth.model.ScoreWc;
import com.jhgk.jhcee.auth.model.ScoreWcExample;
import com.jhgk.jhcee.common.enums.BasisResultEnum;
import com.jhgk.jhcee.common.resultful.BasisResultful;
import com.jhgk.jhcee.common.resultful.PageResultful;
import com.jhgk.jhcee.expert.domain.dao.ScoreWcDao;
import com.jhgk.jhcee.expert.domain.model.ScoreWcModel;
import com.jhgk.jhcee.expert.service.ScoreWcService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author: xiezhongping
 * @description:
 * @date: 2021/8/13 10:25.
 */
@Service(value = "ScoreWcServiceImpl")
public class ScoreWcServiceImpl implements ScoreWcService {

    @Autowired
    ScoreWcMapper scoreWcMapper;

    @Autowired
    ScoreWcDao scoreWcDao;

    @Override
    public PageResultful<PageInfo<ScoreWc>> searchAllPage(ScoreWcModel model) {
        if (null == model) {
            return PageResultful.fail(BasisResultEnum.PARAM_IS_NULL);
        }
        ScoreWcExample example = new ScoreWcExample();
        ScoreWcExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(model.getProvinceName())) {
            criteria.andProvinceNameEqualTo(model.getProvinceName());
        }
        if (StringUtils.isNotBlank(model.getProvinceUuid())) {
            criteria.andProvinceUuidEqualTo(model.getProvinceUuid());
        }
        if (null != model.getYear()) {
            criteria.andYearEqualTo(model.getYear());
        }
        if (null != model.getSubject()) {
            criteria.andSubjectEqualTo(model.getSubject());
        }
        if (null != model.getScore()) {
            criteria.andScoreEqualTo(model.getScore());
        }
        if (null != model.getSameNumber()) {
            criteria.andSameNumberEqualTo(model.getSameNumber());
        }
        if (null != model.getWc()) {
            criteria.andWcEqualTo(model.getWc());
        }
        if (StringUtils.isNotBlank(model.getCreateUserId())) {
            criteria.andCreateUserIdEqualTo(model.getCreateUserId());
        }
        PageHelper.startPage(model.getPage(), model.getLimit());
        List<ScoreWc> scoreWcs = scoreWcMapper.selectByExample(example);
        if (scoreWcs.isEmpty()) {
            return PageResultful.fail(BasisResultEnum.QUERY_IS_NULL);
        }
        return PageResultful.success(new PageInfo<>(scoreWcs));
    }

    @Override
    @Transactional
    public BasisResultful<Object> updateBatch(List<ScoreWc> scoreWcs) {
        if (scoreWcs.isEmpty()) {
            return BasisResultful.fail(BasisResultEnum.PARAM_IS_NULL);
        }
        int updateStatus = scoreWcDao.updateBatch(scoreWcs);
        if (updateStatus > 0) {
            return BasisResultful.success(updateStatus);
        }
        return BasisResultful.fail(updateStatus);
    }

    @Override
    @Transactional
    public BasisResultful<Object> addBatch(List<ScoreWc> scoreWcs) {
        if (scoreWcs.isEmpty()) {
            return BasisResultful.fail(BasisResultEnum.PARAM_IS_NULL);
        }
        int addStatus = scoreWcDao.addBatch(scoreWcs);
        if (addStatus > 0) {
            return BasisResultful.success(addStatus);
        }
        return BasisResultful.fail(addStatus);
    }

    @Override
    @Transactional
    public BasisResultful<Object> update(ScoreWc scoreWc) {
        if (null == scoreWc) {
            return BasisResultful.fail(BasisResultEnum.PARAM_IS_NULL);
        }
        ScoreWcExample example = new ScoreWcExample();
        ScoreWcExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(scoreWc.getProvinceName())) {
            criteria.andProvinceNameEqualTo(scoreWc.getProvinceName());
        }
        if (StringUtils.isNotBlank(scoreWc.getProvinceUuid())) {
            criteria.andProvinceUuidEqualTo(scoreWc.getProvinceUuid());
        }
        if (null != scoreWc.getYear()) {
            criteria.andYearEqualTo(scoreWc.getYear());
        }
        if (null != scoreWc.getSubject()) {
            criteria.andSubjectEqualTo(scoreWc.getSubject());
        }
        int updateStatus = scoreWcMapper.updateByExample(scoreWc, example);
        if (updateStatus > 0) {
            return BasisResultful.success(updateStatus);
        }
        return BasisResultful.fail(updateStatus);
    }

    @Override
    @Transactional
    public BasisResultful<Object> delete(ScoreWc scoreWc) {
        if (null == scoreWc) {
            return BasisResultful.fail(BasisResultEnum.PARAM_IS_NULL);
        }
        ScoreWcExample example = new ScoreWcExample();
        ScoreWcExample.Criteria criteria = example.createCriteria();
        if (null != scoreWc.getId()) {
            criteria.andIdEqualTo(scoreWc.getId());
        }
        if (StringUtils.isNotBlank(scoreWc.getUuid())) {
            criteria.andUuidEqualTo(scoreWc.getUuid());
        }
        if (StringUtils.isNotBlank(scoreWc.getProvinceName())) {
            criteria.andProvinceNameEqualTo(scoreWc.getProvinceName());
        }
        if (StringUtils.isNotBlank(scoreWc.getProvinceUuid())) {
            criteria.andProvinceUuidEqualTo(scoreWc.getProvinceUuid());
        }
        if (null != scoreWc.getYear()) {
            criteria.andYearEqualTo(scoreWc.getYear());
        }
        if (null != scoreWc.getSubject()) {
            criteria.andSubjectEqualTo(scoreWc.getSubject());
        }
        int deleteStatus = scoreWcMapper.deleteByExample(example);
        if (deleteStatus > 0) {
            return BasisResultful.success(deleteStatus);
        }
        return BasisResultful.fail(deleteStatus);
    }
}
